package com.tj.shopping.service.impl;

import com.tj.shopping.entity.Address;
import com.tj.shopping.mapper.AddressMapper;

import com.tj.shopping.service.IAddressService;
import com.tj.shopping.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: H
 * @create: 2024-01-06 13:10
 */
public class AddressServiceImpl implements IAddressService {
    private AddressMapper mapper;

    @Override
    public int add(Address address) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(AddressMapper.class);
        int result= mapper.add(address);
        MybatisUtil.close(session);

        return result;


    }

    @Override
    public int remove(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(AddressMapper.class);
        int result= mapper.remove(map);
        MybatisUtil.close(session);

        return result;

    }

    @Override
    public Map<String, Object> findAll(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(AddressMapper.class);
        List<Address>addresses=mapper.findAll(map);

        Map<String,Object>map1=new HashMap<>();
        int total=mapper.total(map);
        map1.put("count",total);

        if(addresses==null||addresses.isEmpty()){
            map1.put("code",600);
            map1.put("msg","暂无数据");
        }else {
            map1.put("code",200);
            map1.put("msg","成功");
        }
        map1.put("data",addresses);
        MybatisUtil.close(session);
        return map1;

    }
}
